/**
 * Created by dmrj on 2016/4/29.
 */
define(['jquery', 'appConst', 'appMessageBox', 'jquery-form/jquery-form.min', 'jquery-validate/jquery.validate.min'], function ($, appConstant, msgBox) {
    return {
        init: function () {
            this.initEvent();
            this.initEmailValid();
        },

        initEvent: function () {
            $('#btn_test_connection').click(function (e) {
                $('#div_test_connection').fadeToggle();
                $('html,body').animate({scrollTop: 0}, 500);
                e.preventDefault();
            });

            $('#lk_addnew').click(function(e){
                $('#email-form' ).removeClass('submited');
                $('#email-form')[0].reset();
                e.preventDefault();
            });

            $('#protocol').change(function() {
                if($(this).val() == 'smtp'){
                    $('#port').val(25);
                }else if($(this).val() == 'smtps') {
                    $('#port').val(465);
                }
            });

            $('#btn_test').click(function () {
                $('#email-form').attr("action","system/email/testEmail");
            });

            $('#btn_save').click(function () {
                $('#email-form').attr("action","system/email/create");
            });

            var status = $('#status').val();
            $('.r-active').each(function () {
                var statusValue = $(this).attr('data-value');
                if (statusValue == status) {
                    $(this).addClass($(this).attr('data-style'));
                }
            });

            $('#lk_confirm').click(function(e){
                $('#email-form' ).removeClass('submited');
                e.preventDefault();
            });

            $('#btn_update').click(function () {
                $('#email-form').attr("action","system/email/update");
            });
        },
        initEmailValid: function () {
            $("#email-form").validate({
                rules:{
                    name: {
                        required: true
                    },
                    sendAddress: {
                        required: true
                    },
                    hostAddress: {
                        required: true
                    },
                    port: {
                        digits : true
                    },
                    timeout: {
                        number : true
                    }
                },
                messages:{
                    name: {
                        required: $.i18n.prop('VALIDATE_REQUIRED', $.i18n.prop('EMAIL_NAME'))
                    },
                    sendAddress: {
                        required: $.i18n.prop('VALIDATE_REQUIRED', $.i18n.prop('EMAIL_SEND_ADDRESS'))
                    },
                    hostAddress: {
                        required: $.i18n.prop('VALIDATE_REQUIRED', $.i18n.prop('EMAIL_HOST_ADDRESS'))
                    },
                    port: {
                        required: $.i18n.prop('VALIDATE_AVALID', $.i18n.prop('EMAIL_PORT'))
                    },
                    timeout: {
                        required: $.i18n.prop('VALIDATE_REQUIRED', $.i18n.prop('EMAIL_TIMEOUT'))
                    }
                },
                submitHandler:function(form){
                    var isTestMail = false;
                    if($(form).attr("action") == 'system/email/testEmail'){
                        isTestMail = true;
                    }
                    $(form).ajaxSubmit({
                        success:function(d){
                            if(d.code == 0){
                                msgBox.errorBox(d.message, d.message != null ? 'MENU_SYSTEM_EMAIL' : 'EXCEPTION_EMAIL_FAIL');
                                return false;
                            }
                            if(isTestMail) {
                                msgBox.successBox({
                                    content: $.i18n.prop('EXCEPTION_EMAIL_SUCCESS')
                                });
                            }else
                                $("#email-form").addClass('submited');
                        },
                        error: function(e){
                            if (e.status == 401) {
                                msgBox.sessionFailureBox();
                            }
                            if (e.status == 500) {
                                msgBox.serverFailureBox();
                            }
                            if (e.status == 404) {
                                msgBox.pathFailureBox();
                            }
                        }
                    });
                },
                errorPlacement:function (error,element) {
                    error.insertAfter(element.parent());
                }
            });
            $('.r-active').click(function(e) {
                $('.r-active').each(function (){
                    $(this).removeClass($(this).attr('data-style'));
                });
                $(this).addClass($(this).attr('data-style'));
                $('#status').val($(this).attr('data-value'));
            });
        }
    }
});